﻿// https://mp.weixin.qq.com/s?__biz=MzUxNjY5NTYxNA==&mid=2247489268&idx=2&sn=3dccbfa246505b30433c40ccae567bbc&scene=21#wechat_redirect
// https://leetcode-cn.com/problems/find-common-characters/

#include <iostream>
#include <vector>
#include <string>

using namespace std;

// struct ListNode {
//     int val;
//     ListNode *next;
//     ListNode() : val(0), next(nullptr) {}
//     ListNode(int x) : val(x), next(nullptr) {}
//     ListNode(int x, ListNode *next) : val(x), next(next) {}
// };

class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
        vector<string> result;
        int hash[26] = {};

        for (int i = 0; i < words[0].size(); i++) {
            hash[words[0][i] - 'a']++;
        }
        
        int other_hash[26] = {};
        for (int i = 1; i < words.size(); i++) {
            memset(other_hash, 0, 26 * sizeof(int));
            for (int j = 0; j < words[i].size(); j++) {
                other_hash[words[i][j] - 'a']++;
            }

            for (int k = 0; k < 26; k++) {
                hash[k] = min(hash[k], other_hash[k]);
            }
        }

        for (int i = 0; i < 26; i++) {
            while (hash[i] != 0) {
                string s(1, i + 'a');
                result.push_back(s);
                hash[i]--;
            }
        }
        return result;
    }
};

int main() {
    Solution obj = Solution();
}